<?php

//// START FUNCTIONS ////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
function strstrBefore($haystack,$needle){
	// A function that emulates strstr's [before] parameter without having PHP 5.3.0 (SP)
	return array_shift(explode($needle,$haystack,2));
}

function GetImgLocation($imgCode){
	// Function to strip out the image URL from the return of the ImageChecker class (SP)
	$imgCode = str_replace('<img src="', '', $imgCode);
	$imgCode = strstrBefore($imgCode, '"');
	return $imgCode;
}

//// END FUNCTIONS ////////////////////////////////////////////////////////////////////////////////////////////////////////////




//// FIND HOW MANY IMAGES ARE AVAILABLE FOR THIS PRODUCT //////////////////////////////////////////////////////////////////////

$imgCount = 0;
for($i = 0; $i < 4; $i++){
	$imgLoc = "/web3g/img/accessories/".$product_stockcode."_".($i+1).".png";
	if(file_exists($imgLoc)){
		$imgCount++;
	}
}

// $imgCount NOW TELLS US HOW MANY IMAGES WE HAVE.
// IMAGES WILL ALWAYS BE SEQUENTIAL (I.E. IF 1 IMAGE IS MISSING, IT WOULD NOT BE THE 1ST,2ND OR 3RD IMAGE, IT WOULD BE THE 4TH.)

//// END IMAGE COUNT ///////////////////////////////////////////////////////////////////////////////////////////////////////////




//// START DISPLAY OF IMAGES ///////////////////////////////////////////////////////////////////////////////////////////////////

// ASSUME WE HAVE ALL IMAGES AT FIRST, AND WORK BACKWARDS.

if($imgCount == 4){

	// Switch statement to determine how to order the thumbnail images
	// Variable Info:
	// $imgSlots 1-3 = thumbnails, $imgMain = large image, $slots = values for forms
	
	switch($_REQUEST['img']){
		case 1:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgSlot2 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_03.jpg", "146", "106", $product_name, "", ""); $imgSlot2 = GetImgLocation($imgSlot2);
			$imgSlot3 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_04.jpg", "146", "106", $product_name, "", ""); $imgSlot3 = GetImgLocation($imgSlot3);
//			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode .".jpg", "146", "106", $product_name, "", "");
			$slots	  = array(2, 3, 4);
			break;
		case 2:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgSlot2 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_03.jpg", "146", "106", $product_name, "", "");	$imgSlot2 = GetImgLocation($imgSlot2);
			$imgSlot3 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_04.jpg", "146", "106", $product_name, "", "");	$imgSlot3 = GetImgLocation($imgSlot3);
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");
			$slots	  = array(1, 3, 4);
			break;
		case 3:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgSlot2 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");	$imgSlot2 = GetImgLocation($imgSlot2);
			$imgSlot3 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_04.jpg", "146", "106", $product_name, "", "");	$imgSlot3 = GetImgLocation($imgSlot3);
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_03.jpg", "146", "106", $product_name, "", "");
			$slots	  = array(1, 2, 4);
			break;
		case 4:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgSlot2 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");	$imgSlot2 = GetImgLocation($imgSlot2);
			$imgSlot3 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_03.jpg", "146", "106", $product_name, "", "");	$imgSlot3 = GetImgLocation($imgSlot3);
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_04.jpg", "146", "106", $product_name, "", "");
			$slots	  = array(1, 2, 3);
			break;
		default:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgSlot2 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_03.jpg", "146", "106", $product_name, "", "");	$imgSlot2 = GetImgLocation($imgSlot2);
			$imgSlot3 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_04.jpg", "146", "106", $product_name, "", "");	$imgSlot3 = GetImgLocation($imgSlot3);
//			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode .".jpg", "146", "106", $product_name, "", "");
			$slots	  = array(2, 3, 4);
			break;
	}
	
	
	// Thumbnail Divs
	$thumb1Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail1\">
	<form method=\"post\" action=\"/moreinfo/$product_stockcode/$product_category/$currentPage[4]\">
		<input type=\"hidden\" name=\"img\" value=\"$slots[0]\" />
		<input type=\"image\" src=\"$imgSlot1\" width=\"48\" height=\"40\" alt=\"\" />
	</form>
	</div>";
	
	$thumb2Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail2\">
	<form method=\"post\" action=\"/moreinfo/$product_stockcode/$product_category/$currentPage[4]\">
		<input type=\"hidden\" name=\"img\" value=\"$slots[1]\" />
		<input type=\"image\" src=\"$imgSlot2\" width=\"48\" height=\"40\" alt=\"\" />
	</form>
	</div>";
	
	$thumb3Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail3\">
	<form method=\"post\" action=\"/moreinfo/$product_stockcode/$product_category/$currentPage[4]\">
		<input type=\"hidden\" name=\"img\" value=\"$slots[2]\" />
		<input type=\"image\" src=\"$imgSlot3\" width=\"48\" height=\"40\" alt=\"\" />
	</form>
	</div>";
}

elseif($imgCount == 3){
	
	// Switch statement to determine how to order the thumbnail images
	// Variable Info:
	// $imgSlots 1-2 = thumbnails, $imgMain = large image, $slots = values for forms
	
	switch($_REQUEST['img']){
		case 1:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgSlot2 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_03.jpg", "146", "106", $product_name, "", ""); $imgSlot2 = GetImgLocation($imgSlot2);
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");
			$slots	  = array(2,3);
			break;
		case 2:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgSlot2 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_03.jpg", "146", "106", $product_name, "", "");	$imgSlot2 = GetImgLocation($imgSlot2);
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");
			$slots	  = array(1, 3);
			break;
		case 3:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgSlot2 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");	$imgSlot2 = GetImgLocation($imgSlot2);
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_03.jpg", "146", "106", $product_name, "", "");
			$slots	  = array(1, 2);
			break;
		default:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgSlot2 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_03.jpg", "146", "106", $product_name, "", "");	$imgSlot2 = GetImgLocation($imgSlot2);
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");
			$slots	  = array(2, 3);
			break;
	}
	
	
	// Thumbnail Divs
	$thumb1Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail1\">
		<form method=\"post\" action=\"/moreinfo/$product_stockcode/$product_category/$currentPage[4]\">
		<input type=\"hidden\" name=\"img\" value=\"$slots[0]\" />
		<input type=\"image\" src=\"$imgSlot1\" width=\"48\" height=\"40\" alt=\"\" />
	</form>
	</div>";
	
	$thumb2Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail2\">
	<form method=\"post\" action=\"/moreinfo/$product_stockcode/$product_category/$currentPage[4]\">
		<input type=\"hidden\" name=\"img\" value=\"$slots[1]\" />
		<input type=\"image\" src=\"$imgSlot2\" width=\"48\" height=\"40\" alt=\"\" />
	</form>
	</div>";
	
	$thumb3Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail3\"></div>";
}

elseif($imgCount == 2){
	
	// Switch statement to determine how to order the thumbnail images
	// Variable Info:
	// $imgSlot = thumbnail, $imgMain = large image, $slots = values for forms
	
	switch($_REQUEST['img']){
		case 1:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");
			$slots	  = array(2);
			break;
		case 2:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");
			$slots	  = array(1);
			break;
		default:
			$imgSlot1 = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_02.jpg", "146", "106", $product_name, "", "");	$imgSlot1 = GetImgLocation($imgSlot1);
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");
			$slots	  = array(2);
			break;
	}
	
	
	// Thumbnail Divs
	$thumb1Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail1\">
	<form method=\"post\" action=\"/moreinfo/$product_stockcode/$product_category/$currentPage[4]\">
		<input type=\"hidden\" name=\"img\" value=\"$slots[0]\" />
		<input type=\"image\" src=\"$imgSlot1\" width=\"48\" height=\"40\" alt=\"\" />
	</form>
	</div>";
	
	$thumb2Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail2\"></div>";
	
	$thumb3Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail3\"></div>";
}

elseif($imgCount == 1){
	
	// IF THERE IS ONLY ONE IMAGE THERE WILL NOT BE ANY THUMBNAILS, JUST A LARGE IMAGE.
	
	switch($_REQUEST['img']){
		case 1:
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");
			break;
		default:
			$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");
			break;
	}
	
	
	// Thumbnail Divs
	$thumb1Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail1\"></div>";
	
	$thumb2Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail2\"></div>";
	
	$thumb3Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail3\"></div>";
}

elseif($imgCount == 0){
	
	// IF THERE IS ONLY ONE IMAGE THERE WILL NOT BE ANY THUMBNAILS, JUST A LARGE IMAGE.
	
//	$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode ."_01.jpg", "146", "106", $product_name, "", "");
	$imgMain  = $ImageChecker->getImg("/web3g/img/accessories/". $product_stockcode .".jpg", "146", "106", $product_name, "", "");
	
	// Thumbnail Divs
	$thumb1Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail1\"></div>";
	$thumb2Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail2\"></div>";
	$thumb3Div = "<div class=\"prodThumbnail\" id=\"prodThumbnail3\"></div>";
}

//// END DISPLAY OF IMAGES ///////////////////////////////////////////////////////////////////////////////////////////////////

?>